/**
 * 初始化棋盘
 * @param {number} size - 棋盘大小 (2^n)
 * @returns {Array<Array<number>>} - 初始化后的棋盘
 */
function createChessboard(size) {
    const board = [];
    for (let i = 0; i < size; i++) {
        board.push(new Array(size).fill(0)); // 初始值为 0
    }
    return board;
}

// 当前骨牌编号
let tile = 0;

/**
 * 棋盘覆盖算法
 * @param {Array<Array<number>>} board - 棋盘
 * @param {number} tr - 特殊格子所在行
 * @param {number} tc - 特殊格子所在列
 * @param {number} dr - 当前棋盘左上角行坐标
 * @param {number} dc - 当前棋盘左上角列坐标
 * @param {number} size - 棋盘当前大小
 */
function coverChessboard(board, tr, tc, dr, dc, size) {
    if (size === 1) {
        return; // 基础情况，棋盘只有一个格子
    }

    // 骨牌编号
    tile++;

    // 子棋盘大小
    const subSize = size / 2;

    // 判断特殊格子在哪个子棋盘
    // 左上子棋盘
    if (tr < dr + subSize && tc < dc + subSize) {
        // 特殊格子在左上
        coverChessboard(board, tr, tc, dr, dc, subSize);
    } else {
        // 左上子棋盘没有特殊格子，用当前骨牌填充右下角
        board[dr + subSize - 1][dc + subSize - 1] = tile;
        coverChessboard(board, dr + subSize - 1, dc + subSize - 1, dr, dc, subSize);
    }

    // 右上子棋盘
    if (tr < dr + subSize && tc >= dc + subSize) {
        coverChessboard(board, tr, tc, dr, dc + subSize, subSize);
    } else {
        board[dr + subSize - 1][dc + subSize] = tile;
        coverChessboard(board, dr + subSize - 1, dc + subSize, dr, dc + subSize, subSize);
    }

    // 左下子棋盘
    if (tr >= dr + subSize && tc < dc + subSize) {
        coverChessboard(board, tr, tc, dr + subSize, dc, subSize);
    } else {
        board[dr + subSize][dc + subSize - 1] = tile;
        coverChessboard(board, dr + subSize, dc + subSize - 1, dr + subSize, dc, subSize);
    }

    // 右下子棋盘
    if (tr >= dr + subSize && tc >= dc + subSize) {
        coverChessboard(board, tr, tc, dr + subSize, dc + subSize, subSize);
    } else {
        board[dr + subSize][dc + subSize] = tile;
        coverChessboard(board, dr + subSize, dc + subSize, dr + subSize, dc + subSize, subSize);
    }
}

// 测试代码
const n = 3; // 棋盘的大小是 2^n x 2^n
const size = Math.pow(2, n);
const board = createChessboard(size);

// 假设特殊格子在 (1, 2)
const specialRow = 1;
const specialCol = 2;

// 覆盖棋盘
coverChessboard(board, specialRow, specialCol, 0, 0, size);

// 打印结果
console.log("棋盘覆盖方案:");
board.forEach(row => console.log(row.map(cell => (cell === 0 ? "." : cell)).join(" ")));
